package com.cc.eduuser.controller;


import com.cc.JwtUtils;
import com.cc.R;
import com.cc.eduuser.entity.UserMember;
import com.cc.eduuser.entity.vo.RegisterVo;
import com.cc.eduuser.service.UserMemberService;
import com.cc.ordervo.UserMemberOrder;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 会员表 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2020-12-05
 */
@RestController
@RequestMapping("/eduuser/member")
//@CrossOrigin
public class UserMemberController {
    @Autowired
    private UserMemberService userMemberService;

    //登录
    @PostMapping("login")  //@RequestBody 需要使用post提交方式
    public R loginUser(@RequestBody UserMember member){
        //调用service方法实现登录
        //返回token值，使用jwt生成
        String token=userMemberService.login(member);
        return R.ok().data("token", token);
    }

    //注册
    @PostMapping("register")
    public R registerUser(@RequestBody RegisterVo registerVo){
        userMemberService.register(registerVo);
        return R.ok();
    }

    //根据token获取用户信息
    @GetMapping("getMemberInfo")
    public R getMemberInfo(HttpServletRequest request){
        //调用jwt工具类的方法，根据request对象获取头信息，返回用户id
        String memberId = JwtUtils.getMemberIdByJwtToken(request);
        //数据库中根据用户id查询用户信息
        UserMember member = userMemberService.getById(memberId);
        return R.ok().data("userInfo", member);
    }

    //根据用户id获取用户信息
    @PostMapping("getUserInfoOrder/{id}")
    public UserMemberOrder getUserInfoOrder(@PathVariable String id){
        UserMember member = userMemberService.getById(id);
        //把member对象里面的值赋值给UserMemberOrder对象
        UserMemberOrder userMemberOrder = new UserMemberOrder();
        BeanUtils.copyProperties(member,userMemberOrder);
        return userMemberOrder;
    }

    //查询某一天的注册人数
    @GetMapping("countRegister/{day}")
    public R countRegister(@PathVariable String day){
        Integer count = userMemberService.countRegisterDay(day);
        return R.ok().data("countRegister",count);
    }


}
